package com.vitaf.cash.app.sdk.analyse.util

import android.app.Application
import android.os.Build
import com.vitaf.cash.app.configs.GombeCreditApiUrl.addCommonParams
import com.vitaf.cash.app.sdk.analyse.GombeCreditAnalyseBuilder
import com.vitaf.cash.app.sdk.analyse.GombeCreditAnalyseUtil
import org.json.JSONObject
import java.util.Locale


/**
 * Firebase
 */
object GombeCreditFirebaseUtil {

    /** firebase app id, 服务端回推firebase事件使用 */
    private var mFirebaseAppId: String = ""

    /** ga4 instance id, 服务端回推firebase事件使用 */
    private var mGa4InstanceId: String = ""

    /**
     * 初始化
     *
     * @param context Application
     */
    fun init(context: Application,success: (String,String) -> Unit) {
        success.invoke(getFirebaseAppId(), getGa4InstanceId())

//        try {
//            if (mFirebaseAppId.isBlank()) {
//                Thread {
//                    if (Looper.getMainLooper() != Looper.myLooper()) {
//                        mFirebaseAppId = FirebaseAnalytics.getInstance(context).firebaseInstanceId
//                        GombeCreditLogger.i("mFirebaseAppId=$mFirebaseAppId")
//
//                        if (mFirebaseAppId.isNotBlank()){
//                            Handler(Looper.getMainLooper()).post {
//                                //回调通知
//                                success.invoke(getFirebaseAppId(), getGa4InstanceId())
//                            }
//                        }
//                    } else {
//                        GombeCreditLogger.i("init Firebase ERROR! Cannot call in the main thread, You must call in the other thread")
//                    }
//                }.start()
//            } else {
//                Handler(Looper.getMainLooper()).post {
//                    //回调通知
//                    success.invoke(getFirebaseAppId(), getGa4InstanceId())
//                }
//            }
//
//            if (mGa4InstanceId.isBlank()) {
//                val gResult: Task<String?> = FirebaseAnalytics.getInstance(context).appInstanceId
//                gResult.addOnSuccessListener {
//                    mGa4InstanceId = it?:""
//                    GombeCreditLogger.i("mGa4InstanceId=$mGa4InstanceId")
//
//                    if (mGa4InstanceId.isNotBlank()){
//                        Handler(Looper.getMainLooper()).post {
//                            //回调通知
//                            success.invoke(getFirebaseAppId(), getGa4InstanceId())
//                        }
//                    }
//                }
//            } else {
//                //回调通知
//                success.invoke(getFirebaseAppId(), getGa4InstanceId())
//            }
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
    }

    /**
     * 保存 firebase 信息
     */
    fun submitFirebaseInfo() {
        val firebasePath = GombeCreditAnalyseBuilder.pathFirebaseInstall.replace("/","")
        val params = JSONObject().addCommonParams().apply {
            put("path", firebasePath)
            put("name", GombeCreditAnalyseBuilder.appPckName)
            put("OSAndVersion", "Android ${Build.VERSION.RELEASE}")
            put("locale", Locale.getDefault().toString())
            put("device", Build.MODEL.toString())
            put("appVersion", GombeCreditAnalyseBuilder.appVersion)
            put("sdkVersion", Build.VERSION.SDK_INT.toString())
            put("device", "Build/${Build.ID}")
            put("lat", GombeCreditAnalyseUtil.getAdEnabled().toString())
            put("firebaseAppId", getFirebaseAppId())
            put("ga4AppInstanceId", getGa4InstanceId())
        }
        val body = params.toString()
        val url = UrlHelper.combineUrl()
        if (url.isEmpty()){
            return
        }
        AnalyseRequestUtil.doPostAction(url,body)
    }

    /**
     * 获取Firebase App Id
     */
    private fun getFirebaseAppId(): String {
        return mFirebaseAppId
    }

    /**
     * 获取Firebase Ga4 instance Id
     */
    private fun getGa4InstanceId(): String {
        return mGa4InstanceId
    }
}